import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:landlore/consts.dart';
import 'package:landlore/dbUtil/databaseHelper.dart';
import 'package:landlore/model/orderVO.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/***
 * 抄表页面
 */
class CheckingEleWaterScreen extends StatefulWidget {
  final int? orderId;

  final int roomId;
  final String roomNum;
  final int electricityAmount;
  final int waterAmount;
  final String eleImage;
  final String waterImage;

  final int currentYear;
  final int currentMonth;

  final int checkState;

  CheckingEleWaterScreen({
    super.key,
    this.orderId,
    required this.roomId,
    required this.roomNum,
    required this.currentYear,
    required this.currentMonth,
    required this.electricityAmount,
    required this.waterAmount,
    required this.eleImage,
    required this.waterImage,
    required this.checkState,
  });

  @override
  State<CheckingEleWaterScreen> createState() => _CheckingEleWaterScreenState();
}

class _CheckingEleWaterScreenState extends State<CheckingEleWaterScreen> {
  int electricityAmountLastMonth = 0;

  int waterAmountLastMonth = 0;

  late int electricitySelected;
  late int waterSelected;

  //电表选中值辅助List
  List<int> eList = [0, 0, 0, 0, 0];

  //水表选中值辅助List
  List<int> wList = [0, 0, 0, 0, 0];

  FixedExtentScrollController e1 = FixedExtentScrollController();
  FixedExtentScrollController e2 = FixedExtentScrollController();
  FixedExtentScrollController e3 = FixedExtentScrollController();
  FixedExtentScrollController e4 = FixedExtentScrollController();
  FixedExtentScrollController e5 = FixedExtentScrollController();

  FixedExtentScrollController w1 = FixedExtentScrollController();
  FixedExtentScrollController w2 = FixedExtentScrollController();
  FixedExtentScrollController w3 = FixedExtentScrollController();
  FixedExtentScrollController w4 = FixedExtentScrollController();
  FixedExtentScrollController w5 = FixedExtentScrollController();

  List<FixedExtentScrollController> eContrllerList = [];
  List<FixedExtentScrollController> wContrllerList = [];

  final picker = ImagePicker();
  String? eleImgPath;
  String? waterImgPath;

  // XFile? image;

  @override
  void initState() {
    //组装controller
    eContrllerList
      ..add(e1)
      ..add(e2)
      ..add(e3)
      ..add(e4)
      ..add(e5);
    wContrllerList
      ..add(w1)
      ..add(w2)
      ..add(w3)
      ..add(w4)
      ..add(w5);
    //初始化数据
    _getPageData();

    // TODO: implement initState
    super.initState();
  }

  @override
  dispose() {
    e1.dispose();
    e2.dispose();
    e3.dispose();
    e4.dispose();
    e5.dispose();

    w1.dispose();
    w2.dispose();
    w3.dispose();
    w4.dispose();
    w5.dispose();

    super.dispose();
  }

  _getPageData() async {
    //更新获取上月抄表数据
    OrderVO lastMonthOrder = await DatabaseHelper.instance
        .getLastMonthDataByRoomId_DateTime(
            widget.roomId, DateTime(widget.currentYear, widget.currentMonth));
    electricityAmountLastMonth = lastMonthOrder.electricityAmount!;
    waterAmountLastMonth = lastMonthOrder.waterAmount!;

    //初始化最终选定结果
    //若已经抄表,则显示已有结果(若为null,已赋值为0)
    if (widget.electricityAmount != 0) {
      electricitySelected = widget.electricityAmount;
      //若已抄表,检测是否已上传照片
      if (widget.eleImage.isNotEmpty) {
        eleImgPath = widget.eleImage;
      }
    } else {
      electricitySelected = lastMonthOrder.electricityAmount!;
    }
    if (widget.waterAmount != 0) {
      waterSelected = widget.waterAmount;
      //若已抄表,检测是否已上传照片
      if (widget.waterImage.isNotEmpty) {
        waterImgPath = widget.waterImage;
      }
    } else {
      waterSelected = lastMonthOrder.waterAmount!;
    }

    //初始化电表水表,取值为上月的数值,若没有,这为00000
    initalBillData(electricitySelected, 1);
    initalBillData(waterSelected, 2);
    setState(() {});
  }

  initalBillData(int amount, int type) {
    List<int> dataList = [0, 0, 0, 0, 0];
    int length = amount.toString().length;
    //个位
    if (length >= 1) {
      int unit = amount % 10;
      dataList[4] = unit;
    }
    //十位
    if (length >= 2) {
      int ten = (amount ~/ 10) % 10;
      dataList[3] = ten;
    } else {
      dataList[3] = 0;
      dataList[2] = 0;
      dataList[1] = 0;
      dataList[0] = 0;
    }
    //百位
    if (length >= 3) {
      int hundred = (amount ~/ 100) % 10;
      dataList[2] = hundred;
    } else {
      dataList[2] = 0;
      dataList[1] = 0;
      dataList[0] = 0;
    }
    //千位
    if (length >= 4) {
      int thousand = (amount ~/ 1000) % 10;
      dataList[1] = thousand;
    } else {
      dataList[1] = 0;
      dataList[0] = 0;
    }
    //万位
    if (length >= 5) {
      int tenThousand = (amount ~/ 10000) % 10;
      dataList[0] = tenThousand;
    } else {
      dataList[0] = 0;
    }

    switch (type) {
      case 1:
        //初始化组件值
        eContrllerList[0].jumpToItem(dataList[0]);
        eContrllerList[1].jumpToItem(dataList[1]);
        eContrllerList[2].jumpToItem(dataList[2]);
        eContrllerList[3].jumpToItem(dataList[3]);
        eContrllerList[4].jumpToItem(dataList[4]);
        //初始化选中值数组
        eList = dataList;
        break;
      case 2:
        //初始化组件值
        wContrllerList[0].jumpToItem(dataList[0]);
        wContrllerList[1].jumpToItem(dataList[1]);
        wContrllerList[2].jumpToItem(dataList[2]);
        wContrllerList[3].jumpToItem(dataList[3]);
        wContrllerList[4].jumpToItem(dataList[4]);
        //初始化选中值数组
        wList = dataList;
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MediaQuery(
      ///设置文字大小不随系统设置改变
      data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: const Color(0xffe9c46a),
          leading: IconButton(
            icon: const Icon(
              Icons.arrow_back_ios,
              color: Colors.black,
            ),
            onPressed: () => Navigator.pop(context),
          ),
          title: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                widget.roomNum,
                style: Consts.titleText.copyWith(fontSize: 40.sp),
              ),
              Text(
                '${widget.currentYear}.${widget.currentMonth}月',
                style: TextStyle(fontSize: 25.sp, color: Colors.black54),
              ),
            ],
          ),
          // actions: [
          //   IconButton(
          //     icon: const Icon(Icons.save,color: Colors.black,size: 35,),
          //     onPressed: () => Navigator.pop(context),
          //   )
          // ],
          centerTitle: true,
        ),
        body: Container(
          color: const Color(0xfff4a261),
          child: Column(
            children: [
              // const SizedBox(
              //   height: 10,
              // ),
              // //标题行
              // Chip(
              //   labelStyle: const TextStyle(fontSize: 26),
              //   label: Text("${widget.currentYear}.${widget.currentMonth}月"),
              //   backgroundColor: Colors.teal[50],
              // ),
              //
              // const SizedBox(
              //   height: 5,
              // ),
              Stack(
                children: [
                  //电表⚡️卡片
                  Card(
                    color: const Color(0xff309749),
                    elevation: 10,
                    margin: EdgeInsets.only(top: 25.h, bottom: 12.h),
                    child: SizedBox(
                      // height: 250,
                      child: Row(
                        children: [
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    // const Icon(
                                    //   Icons.power_outlined,
                                    //   size: 55,
                                    //   color: Colors.green,
                                    // ),
                                    Text(
                                      "⚡️",
                                      style: TextStyle(fontSize: 45.sp),
                                    ),
                                    Text(
                                      "上月:",
                                      style: TextStyle(fontSize: 30.sp),
                                    ),
                                    Text('$electricityAmountLastMonth',
                                        style: TextStyle(
                                            fontSize: 45.sp,
                                            letterSpacing: 15.sp)),
                                  ],
                                ),
                                Container(
                                  height: 150.h,
                                  padding:
                                      EdgeInsets.only(left: 5.w, right: 5.w),
                                  // margin: const EdgeInsets.only(left: 5,right: 5),
                                  decoration: BoxDecoration(
                                    border: Border(
                                        top: BorderSide(
                                            color: Colors.black, width: 4.w),
                                        bottom: BorderSide(
                                            color: Colors.black, width: 4.w)),
                                    color: Colors.white70,
                                  ),
                                  child: Row(
                                    children: [
                                      Expanded(
                                          flex: 1,
                                          child: CupertinoPicker(
                                            itemExtent: 60.h,
                                            scrollController: eContrllerList[0],
                                            onSelectedItemChanged: (index) {
                                              eList[0] = index;
                                            },
                                            looping: true,
                                            magnification: 1.2,
                                            children: Consts.numberList,
                                          )),
                                      Expanded(
                                          flex: 1,
                                          child: CupertinoPicker(
                                            itemExtent: 60.h,
                                            scrollController: eContrllerList[1],
                                            onSelectedItemChanged: (index) {
                                              eList[1] = index;
                                            },
                                            looping: true,
                                            magnification: 1.2,
                                            children: Consts.numberList,
                                          )),
                                      Expanded(
                                          flex: 1,
                                          child: CupertinoPicker(
                                            itemExtent: 60.h,
                                            scrollController: eContrllerList[2],
                                            onSelectedItemChanged: (index) {
                                              eList[2] = index;
                                            },
                                            looping: true,
                                            magnification: 1.2,
                                            children: Consts.numberList,
                                          )),
                                      Expanded(
                                          flex: 1,
                                          child: CupertinoPicker(
                                            itemExtent: 60.h,
                                            scrollController: eContrllerList[3],
                                            onSelectedItemChanged: (index) {
                                              eList[3] = index;
                                            },
                                            looping: true,
                                            magnification: 1.2,
                                            children: Consts.numberList,
                                          )),
                                      Expanded(
                                          flex: 1,
                                          child: CupertinoPicker(
                                            itemExtent: 60.h,
                                            scrollController: eContrllerList[4],
                                            onSelectedItemChanged: (index) {
                                              eList[4] = index;
                                            },
                                            looping: true,
                                            magnification: 1.2,
                                            children: Consts.numberList,
                                          )),
                                    ],
                                  ),
                                ),
                                Container(
                                  margin:
                                      EdgeInsets.only(top: 5.h, bottom: 5.h),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      DraftButton(
                                        callback: () {
                                          //判断,如果此时状态已经为电表未抄(相反),则判定为都已经抄完
                                          if (widget.checkState ==
                                              OrderCheckState
                                                  .CHECKING_LACK_ELE) {
                                            createOrder(
                                                OrderCheckState.DEFAULT);
                                          } else {
                                            createOrder(OrderCheckState
                                                .CHECKING_LACK_WATER);
                                          }
                                        },
                                      ),
                                      cameraButton(
                                          picker: picker,
                                          callback: (String path) {
                                            setState(() {
                                              eleImgPath = path;
                                            });
                                          }),
                                      SizedBox(
                                        width: 20.w,
                                      ),
                                      eleImgPath != null
                                          ? GestureDetector(
                                              onTap: () async {
                                                await Navigator.push(
                                                    context,
                                                    MaterialPageRoute(
                                                        builder: (context) =>
                                                            FullScreenImage(
                                                                imagePath:
                                                                    eleImgPath!)));
                                              },
                                              child: Hero(
                                                  tag: '$eleImgPath',
                                                  child: Stack(
                                                    clipBehavior: Clip.none,
                                                    children: [
                                                      ClipRRect(
                                                        borderRadius:
                                                            BorderRadius
                                                                .circular(15.r),
                                                        child: Image.file(
                                                          File(eleImgPath!),
                                                          width: 80.w,
                                                          height: 80.w,
                                                          fit: BoxFit.cover,
                                                        ),
                                                      ),
                                                      Positioned(
                                                          right: -5.w,
                                                          top: -5.w,
                                                          child:
                                                              GestureDetector(
                                                            onTap: () {
                                                              setState(() {
                                                                eleImgPath =
                                                                    null;
                                                              });
                                                            },
                                                            child: Container(
                                                                alignment:
                                                                    Alignment
                                                                        .center,
                                                                width: 26.w,
                                                                height: 26.w,
                                                                decoration:
                                                                    BoxDecoration(
                                                                  color: Colors
                                                                      .redAccent,
                                                                  borderRadius:
                                                                      BorderRadius.all(
                                                                          Radius.circular(
                                                                              20.r)),
                                                                ),
                                                                child: Center(
                                                                    child: Text(
                                                                  '×',
                                                                  style: TextStyle(
                                                                      fontSize:
                                                                          20.sp,
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w900,
                                                                      color: Colors
                                                                          .white),
                                                                ))
                                                                // Icon(Icons.close,color: Colors.black,size: 25.sp,),
                                                                ),
                                                          ))
                                                    ],
                                                  )),
                                            )
                                          : SizedBox(
                                              height: 80.w,
                                              width: 80.w,
                                            ),
                                    ],
                                  ),
                                )
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                  Positioned(
                      right: 0.w,
                      top: 0.h,
                      child: Offstage(
                        offstage: !(widget.checkState ==
                            OrderCheckState.CHECKING_LACK_WATER),
                        child: Transform.rotate(
                            angle: 3.14 / (11),
                            child: Image.asset(
                              'assets/images/checked_tag.PNG',
                              scale: 11,
                            )),
                      )),
                  Positioned(
                      left: 0.w,
                      top: 0.h,
                      child: Text(
                        '电表',
                        style: TextStyle(
                            fontSize: 30.sp,
                            fontWeight: FontWeight.bold,
                            color: Colors.white70),
                      ))
                ],
              ),
              Stack(
                children: [
                  //水表💧行
                  Card(
                    color: const Color(0xff3C83EC),
                    elevation: 10,
                    margin: EdgeInsets.only(top: 20.h, bottom: 12.h),
                    child: SizedBox(
                      // height: 250,
                      child: Row(
                        children: [
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    // const Icon(
                                    //   Icons.water_drop_outlined,
                                    //   size: 55,
                                    //   color: Colors.blue,
                                    // ),
                                    Text(
                                      "💧",
                                      style: TextStyle(fontSize: 45.sp),
                                    ),
                                    Text(
                                      "上月:",
                                      style: TextStyle(fontSize: 30.sp),
                                    ),
                                    Text('$waterAmountLastMonth',
                                        style: TextStyle(
                                            fontSize: 45.sp,
                                            letterSpacing: 15.sp)),
                                  ],
                                ),
                                Container(
                                  height: 150.h,
                                  padding:
                                      EdgeInsets.only(left: 5.w, right: 5.w),
                                  // margin: const EdgeInsets.only(left: 5,right: 5),
                                  decoration: BoxDecoration(
                                    border: Border(
                                        top: BorderSide(
                                            color: Colors.black, width: 4.w),
                                        bottom: BorderSide(
                                            color: Colors.black, width: 4.w)),
                                    color: Colors.white70,
                                  ),
                                  child: Row(
                                    children: [
                                      Expanded(
                                          flex: 1,
                                          child: CupertinoPicker(
                                            itemExtent: 60.h,
                                            scrollController: wContrllerList[0],
                                            onSelectedItemChanged: (index) {
                                              wList[0] = index;
                                            },
                                            looping: true,
                                            magnification: 1.2,
                                            children: Consts.numberList,
                                          )),
                                      Expanded(
                                          flex: 1,
                                          child: CupertinoPicker(
                                            itemExtent: 60.h,
                                            scrollController: wContrllerList[1],
                                            onSelectedItemChanged: (index) {
                                              wList[1] = index;
                                            },
                                            looping: true,
                                            magnification: 1.2,
                                            children: Consts.numberList,
                                          )),
                                      Expanded(
                                          flex: 1,
                                          child: CupertinoPicker(
                                            itemExtent: 60.h,
                                            scrollController: wContrllerList[2],
                                            onSelectedItemChanged: (index) {
                                              wList[2] = index;
                                            },
                                            looping: true,
                                            magnification: 1.2,
                                            children: Consts.numberList,
                                          )),
                                      Expanded(
                                          flex: 1,
                                          child: CupertinoPicker(
                                            itemExtent: 60.h,
                                            scrollController: wContrllerList[3],
                                            onSelectedItemChanged: (index) {
                                              wList[3] = index;
                                            },
                                            looping: true,
                                            magnification: 1.2,
                                            children: Consts.numberList,
                                          )),
                                      Expanded(
                                          flex: 1,
                                          child: CupertinoPicker(
                                            itemExtent: 60.h,
                                            scrollController: wContrllerList[4],
                                            onSelectedItemChanged: (index) {
                                              wList[4] = index;
                                            },
                                            looping: true,
                                            magnification: 1.2,
                                            children: Consts.numberList,
                                          )),
                                    ],
                                  ),
                                ),
                                Container(
                                  margin:
                                      EdgeInsets.only(top: 5.h, bottom: 5.h),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      DraftButton(
                                        callback: () {
                                          //判断,如果此时状态已经为水表未抄(相反),则判定为都已经抄完
                                          if (widget.checkState ==
                                              OrderCheckState
                                                  .CHECKING_LACK_WATER) {
                                            createOrder(
                                                OrderCheckState.DEFAULT);
                                          } else {
                                            createOrder(OrderCheckState
                                                .CHECKING_LACK_ELE);
                                          }
                                        },
                                      ),
                                      cameraButton(
                                          picker: picker,
                                          callback: (String path) {
                                            setState(() {
                                              waterImgPath = path;
                                            });
                                          }),
                                      SizedBox(
                                        width: 20.w,
                                      ),
                                      waterImgPath != null
                                          ? GestureDetector(
                                              onTap: () async {
                                                await Navigator.push(
                                                    context,
                                                    MaterialPageRoute(
                                                        builder: (context) =>
                                                            FullScreenImage(
                                                                imagePath:
                                                                    waterImgPath!)));
                                              },
                                              child: Hero(
                                                  tag: '$waterImgPath',
                                                  child: Stack(
                                                    clipBehavior: Clip.none,
                                                    children: [
                                                      ClipRRect(
                                                        borderRadius:
                                                            BorderRadius
                                                                .circular(15.r),
                                                        child: Image.file(
                                                          File(waterImgPath!),
                                                          width: 80.w,
                                                          height: 80.w,
                                                          fit: BoxFit.cover,
                                                        ),
                                                      ),
                                                      Positioned(
                                                          right: -5.w,
                                                          top: -5.w,
                                                          child:
                                                              GestureDetector(
                                                            onTap: () {
                                                              setState(() {
                                                                waterImgPath =
                                                                    null;
                                                              });
                                                            },
                                                            child: Container(
                                                                alignment:
                                                                    Alignment
                                                                        .center,
                                                                width: 26.w,
                                                                height: 26.w,
                                                                decoration:
                                                                    BoxDecoration(
                                                                  color: Colors
                                                                      .redAccent,
                                                                  borderRadius:
                                                                      BorderRadius.all(
                                                                          Radius.circular(
                                                                              20.r)),
                                                                ),
                                                                child: Center(
                                                                    child: Text(
                                                                  '×',
                                                                  style: TextStyle(
                                                                      fontSize:
                                                                          20.sp,
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w900,
                                                                      color: Colors
                                                                          .white),
                                                                ))
                                                                // Icon(Icons.close,color: Colors.black,size: 25.sp,),
                                                                ),
                                                          ))
                                                    ],
                                                  )),
                                            )
                                          : SizedBox(
                                              height: 80.w,
                                              width: 80.w,
                                            ),
                                    ],
                                  ),
                                )
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                  Positioned(
                      right: 0.w,
                      top: 0.h,
                      child: Offstage(
                        offstage: !(widget.checkState ==
                            OrderCheckState.CHECKING_LACK_ELE),
                        child: Transform.rotate(
                            angle: 3.14 / (11),
                            child: Image.asset(
                              'assets/images/checked_tag.PNG',
                              scale: 11,
                            )),
                      )),
                  Positioned(
                      left: 0.w,
                      top: 0.h,
                      child: Text(
                        '水表',
                        style: TextStyle(
                            fontSize: 30.sp,
                            fontWeight: FontWeight.bold,
                            color: Colors.white70),
                      ))
                ],
              ),
              SizedBox(
                height: 30.h,
              ),
              SizedBox(
                width: 130.w,
                height: 65.h,
                child: ElevatedButton(
                    style: ButtonStyle(
                      elevation: MaterialStateProperty.all(9),
                    ),
                    onPressed: () {
                      createOrder(OrderCheckState.DEFAULT);
                    },
                    child: Text(
                      "保存",
                      style: TextStyle(
                          fontSize: 35.sp, fontWeight: FontWeight.w900),
                    )),
              )
            ],
          ),
        ),
      ),
    );
  }

  createOrder(int checkState) async {
    //汇总电表值
    int eAmount = (eList[0] * 10000) +
        (eList[1] * 1000) +
        (eList[2] * 100) +
        (eList[3] * 10) +
        (eList[4] * 1);
    int wAmount = (wList[0] * 10000) +
        (wList[1] * 1000) +
        (wList[2] * 100) +
        (wList[3] * 10) +
        (wList[4] * 1);

    if (eleImgPath != null) {
      //若传参中的图片路径 与 最新路径一致,这不操作
      if (widget.eleImage == eleImgPath) {
        // 创建一个临时文件
        final tempDirectory = (await getApplicationDocumentsDirectory()).path;
        // 将截图数据写入临时文
        // final imageFile = await file!.create();
        final savedImage = await File(eleImgPath!).copy(
            '$tempDirectory/${widget.roomId}_ele_${widget.currentYear}_${widget.currentMonth}.jpg');
        //打印保存的路径
        print(savedImage);
        eleImgPath = savedImage.path;
      }
    }

    if (waterImgPath != null) {
      //若传参中的图片路径 与 最新路径一致,这不操作
      if (widget.eleImage == eleImgPath) {
        // 创建一个临时文件
        final tempDirectory = (await getApplicationDocumentsDirectory()).path;
        // 将截图数据写入临时文
        // final imageFile = await file!.create();
        final savedImage = await File(waterImgPath!).copy(
            '$tempDirectory/${widget.roomId}_water_${widget.currentYear}_${widget.currentMonth}.jpg');
        //打印保存的路径
        print(savedImage);
        waterImgPath = savedImage.path;
      }
    }

    await DatabaseHelper.instance.createOrder(
        widget.orderId,
        widget.roomId,
        widget.roomNum,
        widget.currentYear,
        widget.currentMonth,
        eAmount,
        electricityAmountLastMonth,
        wAmount,
        waterAmountLastMonth,
        eleImgPath ?? eleImgPath,
        waterImgPath ?? waterImgPath,
        checkState);
    Navigator.pop(context);
  }
}

class DraftButton extends StatelessWidget {
  final Function callback;

  const DraftButton({
    super.key,
    required this.callback,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(right: 20.w),
      child: ElevatedButton(
          style: const ButtonStyle(
              backgroundColor: MaterialStatePropertyAll(Color(0xffffc300))),
          onPressed: () {
            callback();
          },
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Icon(
                Icons.fact_check,
                size: 50.sp,
              ),
              Text(
                '暂存',
                style: TextStyle(
                    fontSize: 20.sp,
                    color: Colors.black54,
                    fontWeight: FontWeight.w600),
              ),
            ],
          )),
    );
  }
}

class FullScreenImage extends StatefulWidget {
  final String imagePath;

  FullScreenImage({required this.imagePath});

  @override
  State<FullScreenImage> createState() => _FullScreenImageState();
}

class _FullScreenImageState extends State<FullScreenImage> {
  double scale = 1;
  double previousScale = 1;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.pop(context);
      },
      onScaleStart: (details) {
        previousScale = scale;
      },
      onScaleUpdate: (details) {
        setState(() {
          scale = previousScale * details.scale;
        });
      },
      child: Container(
        color: Colors.black,
        alignment: Alignment.center,
        child: Hero(
          tag: widget.imagePath,
          child: Transform.scale(
            scale: scale,
            child: Image.file(
              File(widget.imagePath),
              fit: BoxFit.contain,
            ),
          ),
        ),
      ),
    );
  }
}

class cameraButton extends StatelessWidget {
  final ImagePicker picker;
  final Function callback;

  const cameraButton({
    super.key,
    required this.picker,
    required this.callback,
  });

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
        style: const ButtonStyle(
            backgroundColor: MaterialStatePropertyAll(Color(0xff90e0ef))),
        onPressed: () {
          showCupertinoModalPopup(
            context: context,
            builder: (BuildContext context) {
              return CupertinoActionSheet(
                // title: Text(''),
                actions: <Widget>[
                  CupertinoActionSheetAction(
                    child: const Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Expanded(
                            child: Text(
                          '拍照',
                          style: TextStyle(color: Colors.black54),
                        )),
                        Icon(
                          Icons.camera_alt,
                          color: Colors.black54,
                        )
                      ],
                    ),
                    onPressed: () async {
                      final image = await picker.pickImage(
                          source: ImageSource.camera, imageQuality: 50);
                      if (image != null) {
                        // //裁剪,有系统bug
                        // String? path = await cropImage(image.path,context);
                        // print(path);

                        callback(image.path);
                      }
                      Navigator.of(context).pop();
                    },
                  ),
                  CupertinoActionSheetAction(
                    child: const Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Expanded(
                            child: Text(
                          '从相册获取',
                          style: TextStyle(color: Colors.black54),
                        )),
                        Icon(
                          Icons.camera,
                          color: Colors.black54,
                        )
                      ],
                    ),
                    onPressed: () async {
                      final image = await picker.pickImage(
                          source: ImageSource.gallery, imageQuality: 50);
                      if (image != null) {
                        callback(image.path);
                      }
                      Navigator.of(context).pop();
                    },
                  ),
                ],
                cancelButton: CupertinoActionSheetAction(
                  child: const Text('取消'),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              );
            },
          );
        },
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Icon(
              Icons.camera_alt_outlined,
              size: 50.sp,
            ),
            Padding(
              padding: EdgeInsets.only(bottom: 5.0.h),
              child: Text(
                '存照',
                style: TextStyle(
                    fontSize: 20.sp,
                    color: Colors.black54,
                    fontWeight: FontWeight.w600),
              ),
            )
          ],
        ));
  }

// Future<String?> cropImage(String path,BuildContext context) async {
//   final croppedFile = await ImageCropper().cropImage(
//     sourcePath: path,
//     compressFormat: ImageCompressFormat.jpg,
//     compressQuality: 100,
//     uiSettings: [
//       AndroidUiSettings(
//           toolbarTitle: 'Cropper',
//           toolbarColor: Colors.deepOrange,
//           toolbarWidgetColor: Colors.white,
//           initAspectRatio: CropAspectRatioPreset.original,
//           lockAspectRatio: false),
//       IOSUiSettings(
//         title: 'Cropper',
//       ),
//       WebUiSettings(
//         context: context,
//         presentStyle: CropperPresentStyle.dialog,
//         boundary: const CroppieBoundary(
//           width: 520,
//           height: 520,
//         ),
//         viewPort:
//         const CroppieViewPort(width: 480, height: 480, type: 'circle'),
//         enableExif: true,
//         enableZoom: true,
//         showZoomer: true,
//       ),
//     ],
//   );
//   return croppedFile?.path;
// }
}
